package guis;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import datacontainers.Category;
import datacontainers.Dish;
import datacontainers.Menu;
import datacontainers.Order;
import datacontainers.OrderItem;

abstract public class AbstractTableGUI extends AbstractGUI {

    protected Table table;

    // HashMap<Dish, TableItem> items = new HashMap<Dish, TableItem>();

    public void createTable(String[] columns) {
	table = new Table(centerShell, SWT.BORDER | SWT.FULL_SELECTION);

	table.setHeaderVisible(true);
	table.setLinesVisible(true);
	table.setEnabled(false);

	for (String c : columns) {
	    TableColumn newColumn = new TableColumn(table, SWT.NONE);
	    newColumn.setWidth(200);// as default
	    newColumn.setText(c);
	}

    }

    public void showMenu(Menu menu) {
	for (Category cat : Category.values()) {
	    addCategoryItem(cat);
	    for (Dish item : menu.getCategory(cat)) {
		addTableItem(item);
	    }
	}

    }

    public void dishToTableItem(Dish dish, TableItem t) {
	// t.setText(0, String.valueOf(o.getDish().getId()));
	t.setText(1, dish.getName());
	t.setText(2, dish.getDescription());
	t.setText(3, String.valueOf(dish.getCost()));
	t.setText(4, "0");
	t.setText(5, "0");

    }

    public void addCategoryItem(Category category) {

	TableItem typeItem = new TableItem(table, SWT.BOLD);
	typeItem.setData("category", category);
	typeItem.setFont(new Font(centerShell.getDisplay(), "Arial", 10,
		SWT.BOLD));

	typeItem.setText(1, category.toString());
    }

    public void addTableItem(Dish item) {
	TableItem positionItem = new TableItem(table, SWT.NONE);
	dishToTableItem(item, positionItem);
	positionItem.setData("dish", item);
	// items.put(item, positionItem);

    }

    public void applyOrderToMenu(Order order) {
	for (TableItem d : table.getItems()) {
	    Dish dish = (Dish) d.getData("dish");
	    if (dish != null) {
		dishToTableItem(dish, d);
		for (OrderItem i : order.getItems()) {
		    if (dish.equals(i.getDish())) {
			d.setText(4, String.valueOf(i.getCount()));
			d.setText(
				5,
				String.valueOf(i.getCount()
					* i.getDish().getCost()));
		    }
		}
	    }
	}
    }
}
